/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import DTO.VaiTro_DTO;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Phung
 */
public class VaiTro_DAO {
    
    public static String Get_MaVT() throws SQLException{
        String MaVT = null;
        MySQLDataHelper_DAO.open();
        Connection Con = MySQLDataHelper_DAO.getConnection();
        CallableStatement statement = Con.prepareCall("{call Add_CT_ID(?)}");
        statement.setString(1, "VT");
        statement.execute();
        ResultSet Rs = statement.getResultSet();
        if(Rs.next()){
            MaVT = Rs.getString(1);
        }
        return MaVT;
    }
    
    public static ArrayList<VaiTro_DTO> GetALL()
    {
        ArrayList<VaiTro_DTO> ds = new ArrayList<>();
        try {
            String sql = "Select * from VaiTro" ;
            MySQLDataHelper_DAO.open();
            ResultSet rs =  MySQLDataHelper_DAO.executeQuery(sql);
            while (rs.next()) {
                VaiTro_DTO VaiTro = new VaiTro_DTO();
                VaiTro.setMaVT(rs.getString("MaVT"));
                VaiTro.setTenVT(rs.getString("TenVT"));
                VaiTro.setDienGiai(rs.getString("DienGiai"));
                VaiTro.setActive(rs.getBoolean("Active"));
                ds.add(VaiTro);
            }
            MySQLDataHelper_DAO.close();
       }catch (SQLException ex) {
            System.out.println(ex);
        }
        return ds;
    }
    
    public static boolean Insert(VaiTro_DTO VaiTro)
    {
        boolean flag = false;
        try {
            String sql = "Insert VaiTro Values('"+VaiTro.getMaVT()+"','"+VaiTro.getTenVT()+"','"+VaiTro.getDienGiai()+"','"+VaiTro.isActive()+"')";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
    public static boolean Update(VaiTro_DTO VaiTro)
    {
        boolean flag = false;
        try {
            String sql = "Update VaiTro Set TenVT = '"+ VaiTro.getTenVT() +"', DienGiai = '"+VaiTro.getDienGiai()+"', Active = "+VaiTro.isActive()+" where MaVT = '"+ VaiTro.getMaVT()+"'";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
    public static boolean Delete(String maVT)
    {
        boolean flag = false;
        try {
            String sql = "Delete from VaiTro where MaVT = "+ maVT;
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
}
